package com.huaxin.common.media.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.R;
import com.huaxin.common.media.ImageGalleryActivity;
import com.huaxin.common.media.SelectImageActivity;
import com.huaxin.common.media.bean.Image;
import com.huaxin.common.media.vidio.CustomRecordActivity;
import com.huaxin.common.tools.Log;
import com.huaxin.common.view.BottomSheetDialog;
import com.luck.picture.lib.PictureVideoPlayActivity;

import java.io.File;
import java.util.List;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.MyViewHolder> implements SelectImageActivity.Callback, CustomRecordActivity.VideoCallBack {

    private              int maxSize   = 8;
    private static final int TYPE_NONE = 0;
    private static final int TYPE_ADD  = 1;
    private final LayoutInflater inflater;
    private final Context        mContext;
    private       List<String>   mDatas;
    private boolean isVideo = false;


    private List<String> mCompress;

    private List<Image> mSelectImages;
    private boolean isRecorderVideo = false;
    private BottomSheetDialog bottomDialog;

    public PhotoAdapter(Context context, List datas, List<Image> selectedImage) {
        mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.mDatas = datas;
        this.mSelectImages = selectedImage;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(inflater.inflate(R.layout.item_photo, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.update(position, getItemViewType(position));
    }

    @Override
    public int getItemViewType(int position) {
        int size = mDatas.size();
        if (position == size) {
            return TYPE_ADD;
        } else {
            return TYPE_NONE;
        }
    }


    @Override
    public int getItemCount() {
        if (mDatas == null) {
            return 0;
        } else {
            return mDatas.size() < maxSize ? mDatas.size() + 1 : maxSize;
        }
    }


    public String[] getPaths() {
        int size = mDatas.size();
        if (size == 0) {
            return null;
        }
        String[] paths = new String[size];
        int i = 0;
        for (String data : mDatas) {
            paths[i++] = data;
        }
        return paths;
    }


    public List<String> getCompressPaths() {
        return mCompress;
    }

    @Override
    public void doSelectDone(String[] images, String[] compress) {
        isVideo = false;
        setMaxSize(4);
        mDatas.clear();
        for (String path : images) {
            mDatas.add(path);
        }
        mCompress.clear();
        for (String path : compress) {
            mCompress.add(path);
        }
        notifyDataSetChanged();
    }

    @Override
    public void recordVideoDone(String path) {
        //视频录制限制一个
        isVideo = true;
        setMaxSize(1);
        mDatas.clear();
        mDatas.add(path);
        notifyDataSetChanged();
    }

    public void addPhoto() {
        SelectImageActivity.selectImages(mContext, maxSize, true, getPaths(), this);
    }

    private void addVideo() {
        //  CustomRecordActivity.recordVideo(mContext, this);
        CustomRecordActivity.addRecordVideoCallback(this);
        Intent intent = new Intent(mContext, CustomRecordActivity.class);
        mContext.startActivity(intent);
    }


    private void viewPhoto(int position) {
        if (isVideo) {
            //预览视频 借助于ictrueSelector中的类
            Intent intent = new Intent(mContext, PictureVideoPlayActivity.class);
            intent.putExtra("video_path", mDatas.get(0));
            mContext.startActivity(intent);
        } else {
            ImageGalleryActivity.show(mContext, getPaths(), position, false);
        }
    }


    public void setDatas(List<String> datas, List<String> compress) {
        this.mDatas = datas;
        this.mCompress = compress;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * 是否需要录制视频
     *
     * @param isRecorderVideo
     */
    public void isRecorderVideo(boolean isRecorderVideo) {
        this.isRecorderVideo = isRecorderVideo;
    }

    /**
     * 判断当前是视频还是图片
     * @return
     */
    public boolean isVidoe(){
        return isVideo;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        net.qiujuer.genius.ui.widget.ImageView file;
        ImageView                              delete;
        ImageView                              play;
        View                                   itemView;


        public MyViewHolder(View itemView) {
            super(itemView);
            file = (net.qiujuer.genius.ui.widget.ImageView) itemView.findViewById(R.id.file);
            delete = (ImageView) itemView.findViewById(R.id.delete);
            play = (ImageView) itemView.findViewById(R.id.play);
            this.itemView = itemView;
            itemView.setId(R.id.itemView);
        }

        public void update(int position, int itemViewType) {

            if (itemViewType == TYPE_NONE) {
                File f = new File(mDatas.get(position));
                Glide.with(mContext).load(Uri.fromFile(f)).apply(new RequestOptions().centerCrop()).into(file);
                //Glide.with(mContext).load(Uri.fromFile(f)).centerCrop().into(file);
                delete.setVisibility(View.VISIBLE);
                if (isVideo) {
                    play.setVisibility(View.VISIBLE);
                } else {
                    play.setVisibility(View.GONE);
                }
            } else {
                file.setImageResource(R.drawable.icon_photo_add);
                play.setVisibility(View.GONE);
                delete.setVisibility(View.GONE);
            }

            delete.setTag(delete.getId(), position);
            delete.setOnClickListener(this);
            file.setTag(file.getId(), position);
            file.setOnClickListener(this);
        }

        public final String[] array = {"短视频录制", "拍照或相册"};

        @Override
        public void onClick(View v) {
            int i = v.getId();
            int position = (int) v.getTag(i);
            if (i == R.id.file) {
                //图片和视频不能同时支持
                Log.e("lwjing", "isrecorderVideo=" + isRecorderVideo);
                if (isRecorderVideo && (mDatas == null || mDatas.size() == 0)) {
                    //弹出底部弹框
                    if (bottomDialog == null) {
                        bottomDialog = new BottomSheetDialog(mContext, array, new BottomSheetDialog.OnItemClickListener() {
                            @Override
                            public void onItemClick(int position) {
                                if (position == 0) {
                                    addVideo();
                                } else {
                                    setMaxSize(4);
                                    addPhoto();
                                }
                            }
                        });
                    }
                    bottomDialog.show();
                } else {
                    if (position < mDatas.size()) {
                        //查看图片
                        viewPhoto(position);
                    } else {
                        //添加图片
                        addPhoto();
                    }
                }

            } else if (i == R.id.delete) {
                //删除图片
                mDatas.remove(position);
                notifyDataSetChanged();

            }
        }

    }


}
